package com.lhk.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
//localhost:8080/3/hello/
@RequestMapping("/hello")
public class HelloController {

    //localhost:8080/3/hello/h1
    @RequestMapping("/h1")
     public String hello(Model model){
         //封装数据
         model.addAttribute("msg","Hello,SpringMVCAnnotation");
         //会被视图解析器处理，返回到hello.jsp
         return "hello";
     }
     //原来的访问方式:http://localhost:8080/3/hello/add?a=1&b=2
     //RestFul方式:http://localhost:8080/3/hello/add/1/2
    @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int res = a + b;
        //封装数据
        model.addAttribute("msg","结果为:"+res);
        //会被视图解析器处理，返回到hello.jsp
        return "hello";
    }
}
